package util

import (
	"github.com/golang-jwt/jwt"
)

type Token struct {
	Token     string `json:"token"`
	ExpiresIn int64  `json:"expires_in"`
	Type      string `json:"type"`
}

func GenerateToken(secret string, iat, expires, uid int64) (Token, error) {
	resp := Token{}

	claims := make(jwt.MapClaims)
	claims["exp"] = iat + expires
	claims["iat"] = iat
	claims["uid"] = uid

	token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
	t, err := token.SignedString([]byte(secret))
	if err != nil {
		return resp, err
	}

	resp = Token{
		Token:     t,
		ExpiresIn: iat + expires,
		Type:      "Bearer ",
	}
	return resp, nil
}
